//
//  YYLanguageVC.m
//  CollarMicrophone
//
//  Created by Yown on 2024/4/15.
//

#import "YYLanguageVC.h"
#import "YYLanguageView.h"
#import "YYMineCellModel.h"

#import <BIProjectModule/BIAppDefine.h>
#import <BIToolModule/BIUserDefault.h>
#import <BIToolModule/NSArray+Helper.h>
#import <BIToolModule/NSDictionary+Helper.h>
#import <BIToolModule/NSMutableArray+Helper.h>
#import <Masonry/Masonry.h>

static NSString *const YYLanguageSettingIndex = @"YYLanguageSettingIndex";

@interface YYLanguageVC ()

@property (nonatomic) YYLanguageView *languageView;
@property (nonatomic) NSMutableArray <YYMineCellModel *>*cellArray;

@end

@implementation YYLanguageVC

#pragma mark - Life Cycle

+ (void)load {
    [[BIRouteManager sharedInstance] registerCMD:@"language" objectOrClass:self];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupDatas];
    [self setupViews];
    [self setupLayouts];
}

- (void)setupDatas {
    // 获取上次选中的index
    NSUInteger index = [[[BIUserDefault sharedInstance] objectForKey:YYLanguageSettingIndex] unsignedIntegerValue];
    
    NSString *path = [[NSBundle mainBundle] pathForResource:@"language" ofType:@"json"];
    NSData *data = [NSData dataWithContentsOfFile:path];
    NSArray *array = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];

    self.cellArray = [NSMutableArray array];
    for (NSUInteger i = 0; i < array.count; i++) {
        YYMineCellModel *model = [YYMineCellModel new];
        model.title = [[array safeObjectAtIndex:i] safeStringForKey:@"name"];
        model.jumpCmd = [[array safeObjectAtIndex:i] safeStringForKey:@"abbreviation"];
        if (index == i) {
            model.arrowName = @"language_selected";
        }
        [self.cellArray addObject:model];
    }
}

- (void)setupViews {
    self.yy_navigationItem.title = BILocalizedString(@"language_title");
    self.yy_navigationBar.naviBgColor = BIColor(BIAppMainBGColor);

    self.languageView = [YYLanguageView view];
    [self.view addSubview:self.languageView];
    [self.languageView reloadView:self.cellArray];
}

- (void)setupLayouts {
    [self.languageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.mas_equalTo(0);
        make.top.mas_equalTo(BINavigationBarHeight);
    }];
}

#pragma mark - Replace Super Method

- (void)languageChanged:(NSNotification *)notification {
    self.yy_navigationItem.title = BILocalizedString(@"language_title");
}

@end
